package org.jsola.hr.web;

import org.jsola.core.Page;
import org.jsola.core.Result;
import org.jsola.exception.ParamException;
import org.jsola.hr.constant.HrConstants;
import org.jsola.hr.dto.SocDispatchCompanyAddDTO;
import org.jsola.hr.dto.SocDispatchCompanyUpdateDTO;
import org.jsola.hr.dto.SocDispatchEmpFeedBackDTO;
import org.jsola.hr.query.SocDispatchCompanyQuery;
import org.jsola.hr.query.SocDispatchEmpAddQuery;
import org.jsola.hr.query.SocDispatchEmpLessQuery;
import org.jsola.hr.service.ISocDispatchCompanyService;
import org.jsola.hr.vo.*;
import org.jsola.user.core.TokenUser;
import org.jsola.user.core.UserContext;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;


/**
 * 社保派单公司记录表
 *
 * @author zhr
 */
@RestController("hrSocDispatchCompanyController")
@RequestMapping(HrConstants.API_PATH + "/v1/socDispatchCompanies")
public class SocDispatchCompanyController implements ISocDispatchCompanyControllerAPI {

    @Autowired
    private ISocDispatchCompanyService socDispatchCompanyService;

    @Override
    @PostMapping
    public Result<SocDispatchCompanyVO> save(@Valid @RequestBody SocDispatchCompanyAddDTO socDispatchCompanyAddDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 新增社保派单公司记录表权限校验

        return Result.success(
                socDispatchCompanyService.save(socDispatchCompanyAddDTO, tokenUser)
        );
    }


    @Override
    @PutMapping
    public Result<Integer> update(@Valid @RequestBody SocDispatchCompanyUpdateDTO socDispatchCompanyUpdateDTO) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 修改社保派单公司记录表权限校验

        int count = socDispatchCompanyService.update(socDispatchCompanyUpdateDTO, tokenUser);
        if (count == 0) {
            throw new ParamException("更新失败，请刷新页面后重试");
        }
        return Result.success(count);
    }



    @Override
    @DeleteMapping(value = "/{socDispatchCompanyId}")
    public Result<Integer> delete(@PathVariable String socDispatchCompanyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 彻底删除社保派单公司记录表权限校验

        int count = socDispatchCompanyService.deleteByIds(tokenUser, socDispatchCompanyId);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @DeleteMapping
    public Result<Integer> deleteBatch(@RequestBody String[] socDispatchCompanyIds) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 批量彻底删除社保派单公司记录表权限校验

        int count = socDispatchCompanyService.deleteByIds(tokenUser, socDispatchCompanyIds);
        if (count == 0) {
            throw new ParamException("删除失败，请刷新页面后重试");
        }
        return Result.success(count);
    }

    @Override
    @GetMapping(value = "/{socDispatchCompanyId}")
    public Result<SocDispatchCompanyVO> findById(@PathVariable String socDispatchCompanyId) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单公司记录表权限校验

        return Result.success(
            socDispatchCompanyService.selectById(socDispatchCompanyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/listAll")
    public Result<List<SocDispatchCompanyListVO>> listAll(@Valid SocDispatchCompanyQuery socDispatchCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看社保派单公司记录表权限校验

        return Result.success(
            socDispatchCompanyService.select(socDispatchCompanyQuery, tokenUser.getSiteId())
        );
    }


    @Override
    @GetMapping
    public Result<Page<SocDispatchCompanyListVO>> page(@Valid SocDispatchCompanyQuery socDispatchCompanyQuery) {
        TokenUser tokenUser = UserContext.getUser();
        // todo 查看项目权限校验

        return Result.success(
            socDispatchCompanyService.selectPage(socDispatchCompanyQuery, tokenUser)
        );
    }

    @Override
    @GetMapping(value = "/details/{socDispatchCompanyId}")
    public Result<SocDispatchCompanyDetailsVO> findCompanyDetailsById(@PathVariable String socDispatchCompanyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                socDispatchCompanyService.findCompanyDetailsById(socDispatchCompanyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/pageEmpAdd/{socDispatchCompanyId}")
    public Result<Page<SocDispatchEmpAddListVO>> pageEmpAdd(@Valid SocDispatchEmpAddQuery socDispatchEmpAddQuery,
                                                            @PathVariable String socDispatchCompanyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                socDispatchCompanyService.pageEmpAdd(socDispatchEmpAddQuery, socDispatchCompanyId, tokenUser.getSiteId())
        );
    }

    @Override
    @GetMapping(value = "/pageEmpLess/{socDispatchCompanyId}")
    public Result<Page<SocDispatchEmpLessListVO>> pageEmpLess(@Valid SocDispatchEmpLessQuery socDispatchEmpLessQuery,
                                                              @PathVariable String socDispatchCompanyId) {
        TokenUser tokenUser = UserContext.getUser();

        return Result.success(
                socDispatchCompanyService.pageEmpLess(socDispatchEmpLessQuery, socDispatchCompanyId, tokenUser.getSiteId())
        );
    }

}

